package org.tamal.struts;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

/**
 * This validator determines whether the password is strong or not.
 * @author Tamal Kanti Nath
 */
public final class StrongPasswordValidator extends FieldValidatorSupport {

    /**
     * This method validates whether the field value contains upper case, lower
     * case, numeric and special characters or not. If either of above is not
     * found, the method raises field error.
     *
     * @param object the object to be validated.
     * @throws ValidationException is thrown if there is validation error(s).
     */
    @Override
    public void validate(Object object) throws ValidationException {
        String field = getFieldName();
        String str = (String) getFieldValue(field, object);
        if (str == null || str.isEmpty()) {
            return;
        }
        if    (str.matches(".*\\p{Lower}.*")
            && str.matches(".*\\p{Upper}.*")
            && str.matches(".*\\p{Digit}.*")
            && str.matches(".*\\p{Punct}.*")) {
            return;
        }
        addFieldError(field, object);
    }
}
